# -*- coding:utf-8 -*-
import requests
import json
import pymysql
import time
import random

# 初始化mysql连接
connect = pymysql.Connect(
    host="localhost",
    port=3306,
    user="root",
    passwd="123456",
    db="misoft",
    charset="utf8"
)

# 创建mysql游标
cur = connect.cursor()



for category in range(0,30):
    page = 0
    while 1:
        url = "https://app.mi.com/categotyAllListApi?page="+str(page)+"&categoryId="+str(category)+"&pageSize=300"
        print("url:"+url)
        response = requests.get(url)
        time.sleep(1)
        try:
            softinfo = json.loads(response.text)
        except:
            response.close()
            print("连接被重置，重新请求："+url)
            url = "https://app.mi.com/categotyAllListApi?page="+str(page)+"&categoryId="+str(category)+"&pageSize=300"
            print("url:"+url)
            response = requests.get(url)
            softinfo = json.loads(response.text)

        if(len(softinfo['data']) == 0):
            print("该类别爬取完成。。。")
            break
        else:
            page += 1
            for soft in softinfo['data']:
                sql = "insert into softprofile(app_id,name,logo,category,package_name) value (%d,'%s','%s','%s','%s')"
                softdata = (soft['appId'],soft['displayName'],soft['icon'].replace('l62','l114'),soft['level1CategoryName'],soft['packageName'])
                try:
                    cur.execute(sql % softdata)
                    connect.commit()
                except:
                    print('%s数据已存在'%soft)
                    connect.rollback()
        response.close()
